package com.company.reptile;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*描述 网络爬虫  多线程
尝试爬取某个茶座前100页的内容
 * @authro szq
 * @time 2022/01/05*/
public class ReptilerFoundation2 {


    private static Logger log = Logger.getLogger(ReptilerFoundation2.class);
    private static String rootPath = "";
    private static List<String> pageParamList;
    private static volatile String visitedPages = "";
    private static Vector<FoundationDetail> foundationDetailList;
    //    原子更新基本类型 记录爬取的链接个数
    private static AtomicInteger linkcount = new AtomicInteger(0);
    private static Object xlock = new Object();

    public static void main(String[] args) throws InterruptedException {

        String fcode ="008279";
        rootPath="http://fundf10.eastmoney.com/jjjz_" + "008279.html";
//        构造20个页码
        pageParamList = new ArrayList<>();
        for (Integer i = 1; i <= 10; i++) {
            pageParamList.add( i.toString());
        }

        foundationDetailList=new Vector<>();

//        多线程处理
//        创建4个线程 分别去爬取网页
        MyThread myThread1 = new MyThread(rootPath,"t1");
        MyThread myThread2 = new MyThread(rootPath,"t2");
        MyThread myThread3 = new MyThread(rootPath,"t3");
        MyThread myThread4 = new MyThread(rootPath,"t4");
        MyThread myThread5 = new MyThread(rootPath,"t5");
        MyThread myThread6 = new MyThread(rootPath,"t6");

        myThread1.start();
        myThread2.start();
        myThread3.start();
        myThread4.start();
        myThread5.start();
        myThread6.start();

        myThread1.join();
        myThread2.join();
        myThread3.join();
        myThread4.join();
        myThread5.join();
        myThread6.join();

        String[] visitedPagesArray = visitedPages.split(";");

        for (String visitedPage : visitedPagesArray
        ) {
            System.out.println(visitedPage);
        }
        System.out.println("基金代码：008279--》》共爬取" + visitedPagesArray.length + "页！");
        System.out.println("基金代码：008279--》》共爬取" + foundationDetailList.size() + "条记录！");

    }

    public static String getHtml(String url,String pageNum) {
        String htmlStr = "";
        String jsPath = "D:\\java_web\\JavaCourse\\src\\com\\company\\reptile\\myjs.js";
        String exePath = "D:\\java_web\\JavaCourse\\src\\com\\company" +
                "\\reptile\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe";
        System.out.println(jsPath);
        System.out.println(exePath);
        Runtime rt = Runtime.getRuntime();
        Process p;
        try {
            p = rt.exec(exePath + " " + jsPath + " " + url + " " + pageNum);

            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuffer sbf = new StringBuffer();
            String tmp = "";
            while ((tmp = br.readLine()) != null) {
                sbf.append(tmp);
            }
            htmlStr = sbf.toString();

            is.close();
            br.close();
            sbf = null;
            is = null;
            br = null;
        } catch (IOException e) {

            e.printStackTrace();
        }
        return htmlStr;

    }


    /*爬取网页源代码并处理
     * @param requestUrl String 请求地址
     * @return */
    public static void AccessHtml(String urlPath, String threadName,String pageNum) {
        String msg = getHtml(urlPath,pageNum);
        msg = msg.replace("\n", "").replace("\t", "");
        // ?:0|1;  *:0|n  +:1|n+
        String pattern = "<tr class=\"\".+?/tr>";
        Matcher m = Pattern.compile(pattern).matcher(msg);
        while (m.find()) {
//            for (int i = 0; i <= m.groupCount(); i++) {//这里 i<m.groupCount();改成i<=m.groupCount();
            String msg2 = m.group(0);
            // ?:0|1;  *:0|n  +:1|n
            String pattern2 = "<td.*?/td>";
            Matcher m2 = Pattern.compile(pattern2).matcher(msg2);
            FoundationDetail foundationDetail=new FoundationDetail();
            Integer colcount=0;
            while (m2.find()) {
                String msg3 = m2.group(0);
//                    <a href="chazuo/15469178" name="readlink"
//                      id="a_ajax_15469178" class="subject_t fs14"
//                      title="在常州找一个好的中医，难吗？今日话题：说说你遇见过的好中医！" style="">
//                    <b><font color="#FF0000">在常州找一个好的中医，难吗？今日话题：说说你遇见过的好中医！</font></b></a>
//                    String pattern3 = "<a href=\"(.*)\" name=\"readlink\".*title=\"(.*)\" .*/a>";
                // ?:0|1;  *:0|n  +:1|n
                String pattern3 = "<td.*?>(.*?)</td>";
                Matcher m3 = Pattern.compile(pattern3).matcher(msg3);
                while (m3.find()) {
                    colcount=colcount+1;
                    if (colcount==1){
                        foundationDetail.setFsrq(m3.group(1));
                    } else if (colcount==2){
                        foundationDetail.setDwjz(m3.group(1));
                    } else if (colcount==3){
                        foundationDetail.setLjjz(m3.group(1));
                    } else if (colcount==4){
                        foundationDetail.setJzzzl(m3.group(1));
                    } else if (colcount==5){
                        foundationDetail.setSgzt(m3.group(1));
                    } else if (colcount==6){
                        foundationDetail.setShzt(m3.group(1));
                    }
                    System.out.println(m3.group(1));
                }
            }
            foundationDetailList.add(foundationDetail);

//            }
        }
    }



    public void ReptilerFoundation2() {

    }

    public static class MyThread extends Thread {
        private String urlPath;
        private String threadName;
        public MyThread(String urlPath, String threadName) {
            this.urlPath = urlPath;
            this.threadName = threadName;
        }

        @Override
        public void run() {
            // 爬取网页源代码并处理
            for (String pageParam : pageParamList
            ) {
                Boolean flag = false;
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                if (visitedPages.contains(pageParam + ";")) {
                    flag = false;
                } else {
//                    没有爬取过
//                  这里的操作没有原子性，防止重复爬取网页需要加个锁
                    synchronized (xlock) {
//                        try {
//                            Thread.sleep(100);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        这里还要判断一下，类似单例模式
                        if (visitedPages.contains(pageParam + ";")) {
                            flag = false;
                        } else {
                            flag = true;
                            visitedPages = visitedPages + pageParam + ";";
                        }
                    }
                }
                if (flag == true) {
                    AccessHtml(urlPath, threadName,pageParam);
                }
            }
        }
    }


}